#include <iostream>
#include <vector>
#include <queue>

using namespace std;

// Definition for a Node.
class Node 
{
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};


class Solution 
{
public:
    vector<vector<int>> levelOrder(Node* root) 
    {
        vector<vector<int>> result;
        queue<Node *> q;
        if(root == nullptr)
        {
            return result;
        }
        q.push(root);
        int size = 1;
        while(!q.empty())
        {
            int sizeNew = 0;
            vector<int> v;
            for(int i = 0; i < size; i++)
            {
                Node * cur = q.front();
                q.pop();
                v.push_back(cur->val);
                for(int j = 0; j < cur->children.size(); j++)
                {
                    if(cur->children[j] != nullptr)
                    {
                        q.push(cur->children[j]);
                        sizeNew++;
                    }
                }
            }
            size = sizeNew;
            result.push_back(v);
        }
        return result;
    }
};

// 辅助函数：创建N叉树节点
Node* createNode(int val) {
    return new Node(val);
}

// 辅助函数：打印层序遍历结果
void printResult(const vector<vector<int>>& result) {
    cout << "[";
    for (int i = 0; i < result.size(); i++) {
        cout << "[";
        for (int j = 0; j < result[i].size(); j++) {
            cout << result[i][j];
            if (j < result[i].size() - 1) cout << ",";
        }
        cout << "]";
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：释放N叉树内存
void deleteTree(Node* root) {
    if (root == nullptr) return;
    for (Node* child : root->children) {
        deleteTree(child);
    }
    delete root;
}

int main() {
    Solution solution;
    
    // 测试案例1: LeetCode示例1 - [1,null,3,2,4,null,5,6]
    //       1
    //    /  |  \
    //   3   2   4
    //  / \
    // 5   6
    // 预期输出: [[1],[3,2,4],[5,6]]
    cout << "测试案例1: [1,null,3,2,4,null,5,6]" << endl;
    Node* root1 = createNode(1);
    Node* node3 = createNode(3);
    Node* node2 = createNode(2);
    Node* node4 = createNode(4);
    Node* node5 = createNode(5);
    Node* node6 = createNode(6);
    
    root1->children = {node3, node2, node4};
    node3->children = {node5, node6};
    
    vector<vector<int>> result1 = solution.levelOrder(root1);
    cout << "输出: ";
    printResult(result1);
    cout << "预期: [[1],[3,2,4],[5,6]]" << endl << endl;
    
    // 测试案例2: LeetCode示例2 - 更复杂的N叉树
    //         1
    //     /   |   |   \
    //    2    3   4    5
    //        /|\      /|\
    //       6 7 8    9 10 11
    //         |      |
    //        12     13
    // 预期输出: [[1],[2,3,4,5],[6,7,8,9,10,11],[12,13]]
    cout << "测试案例2: 复杂N叉树" << endl;
    Node* root2 = createNode(1);
    Node* n2 = createNode(2);
    Node* n3 = createNode(3);
    Node* n4 = createNode(4);
    Node* n5 = createNode(5);
    Node* n6 = createNode(6);
    Node* n7 = createNode(7);
    Node* n8 = createNode(8);
    Node* n9 = createNode(9);
    Node* n10 = createNode(10);
    Node* n11 = createNode(11);
    Node* n12 = createNode(12);
    Node* n13 = createNode(13);
    
    root2->children = {n2, n3, n4, n5};
    n3->children = {n6, n7, n8};
    n5->children = {n9, n10, n11};
    n7->children = {n12};
    n9->children = {n13};
    
    vector<vector<int>> result2 = solution.levelOrder(root2);
    cout << "输出: ";
    printResult(result2);
    cout << "预期: [[1],[2,3,4,5],[6,7,8,9,10,11],[12,13]]" << endl << endl;
    
    // 测试案例3: 空树
    cout << "测试案例3: []" << endl;
    Node* root3 = nullptr;
    
    vector<vector<int>> result3 = solution.levelOrder(root3);
    cout << "输出: ";
    printResult(result3);
    cout << "预期: []" << endl << endl;
    
    // 测试案例4: 只有一个节点
    cout << "测试案例4: [1]" << endl;
    Node* root4 = createNode(1);
    
    vector<vector<int>> result4 = solution.levelOrder(root4);
    cout << "输出: ";
    printResult(result4);
    cout << "预期: [[1]]" << endl << endl;
    
    // 测试案例5: 只有两层的树
    //   1
    //  /|\
    // 2 3 4
    // 预期输出: [[1],[2,3,4]]
    cout << "测试案例5: [1,null,2,3,4]" << endl;
    Node* root5 = createNode(1);
    Node* c2 = createNode(2);
    Node* c3 = createNode(3);
    Node* c4 = createNode(4);
    
    root5->children = {c2, c3, c4};
    
    vector<vector<int>> result5 = solution.levelOrder(root5);
    cout << "输出: ";
    printResult(result5);
    cout << "预期: [[1],[2,3,4]]" << endl << endl;
    
    // 测试案例6: 链式结构（每个节点只有一个子节点）
    //   1
    //   |
    //   2
    //   |
    //   3
    //   |
    //   4
    // 预期输出: [[1],[2],[3],[4]]
    cout << "测试案例6: 链式结构" << endl;
    Node* root6 = createNode(1);
    Node* l2 = createNode(2);
    Node* l3 = createNode(3);
    Node* l4 = createNode(4);
    
    root6->children = {l2};
    l2->children = {l3};
    l3->children = {l4};
    
    vector<vector<int>> result6 = solution.levelOrder(root6);
    cout << "输出: ";
    printResult(result6);
    cout << "预期: [[1],[2],[3],[4]]" << endl << endl;
    
    // 测试案例7: 不平衡的N叉树
    //     1
    //   / | \
    //  2  3  4
    //  |     |\
    //  5     6 7
    //        |
    //        8
    // 预期输出: [[1],[2,3,4],[5,6,7],[8]]
    cout << "测试案例7: 不平衡N叉树" << endl;
    Node* root7 = createNode(1);
    Node* u2 = createNode(2);
    Node* u3 = createNode(3);
    Node* u4 = createNode(4);
    Node* u5 = createNode(5);
    Node* u6 = createNode(6);
    Node* u7 = createNode(7);
    Node* u8 = createNode(8);
    
    root7->children = {u2, u3, u4};
    u2->children = {u5};
    u4->children = {u6, u7};
    u6->children = {u8};
    
    vector<vector<int>> result7 = solution.levelOrder(root7);
    cout << "输出: ";
    printResult(result7);
    cout << "预期: [[1],[2,3,4],[5,6,7],[8]]" << endl << endl;
    
    // 清理内存
    deleteTree(root1);
    deleteTree(root2);
    deleteTree(root4);
    deleteTree(root5);
    deleteTree(root6);
    deleteTree(root7);
    // root3 是 nullptr，不需要删除
    
    return 0;
}